<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base href="<%=basePath%>">
    <link rel="stylesheet" href="static/component/pear/css/pear.css" />
    <link rel="stylesheet" href="static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="static/css/public.css" media="all">
    <style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 28px;
        }
    </style>
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" >
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">申请单号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="purCode" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">经办人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="eName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">审批状态</label>
                            <div class="layui-input-inline">
                                <select name="purchaseState"  class="layui-input specifiCations" lay-filter="relationship">
                                    <option value="-1">全部</option>
                                    <option value="0">待审批</option>
                                    <option value="2">待采购</option>
                                    <option value="3">待入库</option>
                                    <option value="4">已入库</option>
                                    <option value="5">已驳回</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="shelfApplicationFormSubmit"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <%--<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>--%>
        </script>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="purchase" lay-filter="purchase" class="layui-hide"></table>
            </div>
        </div>
        <%--      <c:if test="${emp.eDepartment==2}">

              </c:if>--%>

        <%--    <shiro:hasAnyRoles name="库管员">
                <c:if test="${}">

                <a class="pear-btn pear-btn-success pear-btn-sm" lay-event="sp"> <i class="layui-icon layui-icon-ok">审批</i></a>

                </c:if>

            </shiro:hasAnyRoles>--%>


    </div>

</div>

</body>
<script type="text/html" id="brandToolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script src="static/component/layui/layui.js"></script>
<script src="static/component/pear/pear.js"></script>

<script>
    //引入table
    layui.use(['table','form'],function(){

        var $ = layui.jquery;
        var from = layui.form;
        var table = layui.table;
        var form = layui.form;
        //执行表格的数据填充操作
        table.render({
            //指定渲染的表格
            elem: '#purchase',
            //指定获取数据的路径
            url: 'purchase/likePurchase',
            toolbar: '#toolbarDemo',
            //规定请求方式
            method: 'post',
            //开启分页
            page: true,
            check: true,
            //指定每页显示数
            limits: [1, 10, 15],
            even: true,
            cols: [[
                {field: 'purCode', title: '申请单编号', align: 'center'},
                {field: 'goodstotalCount', title: '总数量', align: 'center'},
                {field: 'purchaseType', title: '申请单类型', align: 'center',templet: "#purchaseType"},
                {field: 'purchaseState', title: '审批状态',  align: 'center',templet:"#purchaseState"},
                {field: 'supplier', title: '供应商',  align: 'center',templet:"#supplier"},
                {field: 'emp', title: '申请人',  align: 'center',templet:"#emp"},
                {field: 'payState', title: '支付状态',  align: 'center',templet:"#payState"},
                {field: 'repostoryType', title: '操作仓库',  align: 'center',templet:"#repostory"},
                {field: 'createDate', title: '进货日期',  align: 'center'},
                {field: 'capacity', title: '所需容量', align: 'center'},
                {field: 'remarkes', title: '备注',  align: 'center'},
                { title: '操作',  align: 'center',width:220,templet:function (d){
                        //判断条件
                        var state = "";
                        //判断如果是待经理审批
                        if (d.purchaseState==0){
                            state = " <c:if test='${emp.eDepartment.departmentId==3}'>"+
                                "<shiro:hasAnyRoles name='经理,系统管理员'>\n" +
                                "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"jlsp\"> <i class=\"layui-icon layui-icon-ok\">审批</i></a>\n" +
                                "        <a class=\"pear-btn pear-btn-danger pear-btn-sm\" lay-event=\"jlbh\"> <i class=\"layui-icon  layui-icon-close\">驳回</i></a>\n" +
                                "    </shiro:hasAnyRoles>"+
                                "        </c:if>";

                        }else if(d.purchaseState==1){
                            //判断类型是进货还是退货
                            if (d.purchaseType=='进货'){
                                //财务打款审批
                                state = "<shiro:hasAnyRoles name='财务'>\n" +
                                    "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"cwsp\"> <i class=\"layui-icon layui-icon-ok\">财务审批</i></a>\n" +
                                    "        <a class=\"pear-btn pear-btn-danger pear-btn-sm\" lay-event=\"cwbh\"> <i class=\"layui-icon  layui-icon-close\">驳回</i></a>\n" +
                                    "    </shiro:hasAnyRoles>";
                            }else{
                                //退货操作
                                if (d.repostoryType.eid==${emp.eId}){
                                    //等待采购
                                    state = "<shiro:hasAnyRoles name='库管员'>\n" +
                                        "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"ck\"> <i class=\"layui-icon layui-icon-ok\">出库</i></a>\n" +
                                        "    </shiro:hasAnyRoles>";
                                }else{
                                    //不是仓库负责人不可操作
                                    state = "    <shiro:hasAnyRoles name='库管员'>\n" +
                                        "\n" +
                                        "<button type='button' class='pear-btn layui-btn-disabled layui-btn-sm' > <i class='layui-icon-ok layui-icon '>出库</i></button>"+                                "\n" +
                                        "\n" +
                                        "    </shiro:hasAnyRoles>";
                                }
                            }


                        }else if(d.purchaseState==2){
                            //判断类型是进货还是退货
                            if (d.purchaseType=='进货'){
                                //等待采购
                                state = "<shiro:hasAnyRoles name='采购员'>\n" +
                                    "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"cg\"> <i class=\"layui-icon layui-icon-add-1\">采购</i></a>\n" +
                                    "    </shiro:hasAnyRoles>";
                            }else{
                                //退货操作
                                if (d.repostoryType.eid==${emp.eId}){
                                    //等待采购
                                    state = "<shiro:hasAnyRoles name='库管员'>\n" +
                                        "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"ck\"> <i class=\"layui-icon layui-icon-ok\">出库</i></a>\n" +
                                        "    </shiro:hasAnyRoles>";
                                }else{
                                    //不是仓库负责人不可操作
                                    state = "    <shiro:hasAnyRoles name='库管员'>\n" +
                                        "\n" +
                                        "<button type='button' class='pear-btn layui-btn-disabled layui-btn-sm' > <i class='layui-icon-ok layui-icon '>出库</i></button>"+                                "\n" +
                                        "\n" +
                                        "    </shiro:hasAnyRoles>";
                                }


                            }
                        }else if(d.purchaseState==3){
                            if (d.purchaseType=='进货'){
                                if (d.repostoryType.eid==${emp.eId}){
                                    //等待采购
                                    state = "<shiro:hasAnyRoles name='库管员'>\n" +
                                        "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"rk\"> <i class=\"layui-icon layui-icon-ok\">入库</i></a>\n" +
                                        "    </shiro:hasAnyRoles>";
                                }else{
                                    //不是仓库负责人不可操作
                                    state = "    <shiro:hasAnyRoles name='库管员'>\n" +
                                        "\n" +
                                        "<button type='button' class='pear-btn layui-btn-disabled layui-btn-sm' > <i class='layui-icon-ok layui-icon '>入库</i></button>"+                                "\n" +
                                        "\n" +
                                        "    </shiro:hasAnyRoles>";
                                }
                            }else{
                                //退货流程
                                state = "<shiro:hasAnyRoles name='采购员'>\n" +
                                    "        <a class=\"pear-btn pear-btn-success pear-btn-sm\" lay-event=\"th\"> <i class=\"layui-icon   layui-icon-ok \">退货</i></a>\n" +
                                    "    </shiro:hasAnyRoles>";
                            }
                        }

                        return state += " <button class='pear-btn pear-btn-success pear-btn-sm' lay-event='xq'><i class='layui-icon layui-icon-form'></i></button>";
                    }},

    ]]
        })

        //表单提交事件
        form.on("submit(shelfApplicationFormSubmit)",function(data){
            //执行搜索重载
            table.reload('purchase',{
                //规定默认第几页
                page:{
                    curr:1
                },
                where:{
                    //请求参数添加
                    purCode:data.field.purCode,
                    emp:data.field.eName,
                    purchaseState:data.field.purchaseState
                }
            });
            return false;
        })
        table.on("tool(purchase)",function (obj){
            //打开一个页面,展示该申请单的详情信息
            if (obj.event=="xq"){
                layer.open({
                    //执行修改用户操作
                    title: '上架申请单详情',
                    type: 2,
                    shade: 0.1,
                    area: ['50%', '50%'],
                    content: 'purchase/skipPurchaseDetails?purCode='+obj.data.purCode
                })
            }else if(obj.event==="jlsp"){
                layer.confirm('是否同意审批？',function(index) {
                    //发送异步请求 改变审批状态
                    $.post("purchase/purCodeState1",{purCode:obj.data.purCode},function (result){
                        if (result.success){
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                //执行重载
                                table.reload('purchase');
                                layer.close(index);
                            });
                        }
                    })
                })
            }else if(obj.event==="jlbh"){
                //打开一个页面,填写驳回原因
                layer.open({
                    //执行修改用户操作
                    title: '填写驳回原因',
                    type: 2,
                    shade: 0.1,
                    area: ['50%', '30%'],
                    content: 'purchase/skiprejectCause?purCode='+obj.data.purCode
                })


            }else if(obj.event==="cwsp"){
                //财务审批
                layer.confirm('是否同意打款审批？',function(index) {
                    //发送异步请求 改变审批状态
                    $.post("purchase/purCodeState2",{purCode:obj.data.purCode},function (result){
                        if (result.success){
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                //执行重载
                                table.reload('purchase');
                                layer.close(index);
                            });
                        }
                    })
                })
            }else if(obj.event=="cwbh"){
                //财务驳回
                //打开一个页面,填写驳回原因
                layer.open({
                    //执行修改用户操作
                    title: '填写驳回原因',
                    type: 2,
                    shade: 0.1,
                    area: ['50%', '30%'],
                    content: 'purchase/skipRejectCauseCw?purCode='+obj.data.purCode
                })

            }else if(obj.event=="cg"){
                //采购员采购
                layer.confirm('是否要确认采购？',function(index) {
                    //发送异步请求 改变审批状态
                    $.post("purchase/purCodeState3",{purCode:obj.data.purCode},function (result){
                        if (result.success){
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                //执行重载
                                table.reload('purchase');
                                layer.close(index);
                            });
                        }
                    })
                })

            }else if(obj.event==="rk"){
                //出库操作,填写备注
                layer.open({
                    //执行修改用户操作
                    title: '填写入库备注',
                    type: 2,
                    shade: 0.1,
                    area: ['50%', '30%'],
                    content: 'purchase/skipGodowEntry?purCode='+obj.data.purCode
                })
            }else if(obj.event==="ck"){
                layer.open({
                    //执行修改用户操作
                    title: '填写出库备注',
                    type: 2,
                    shade: 0.1,
                    area: ['50%', '30%'],
                    content: 'purchase/skipOutBoundRemarks?purCode='+obj.data.purCode
                })
            }else if(obj.event==="th"){
                layer.confirm('是否要退货？',function(index) {
                    //发送异步请求 执行退货
                    $.post("purchase/purCodeState4",{purCode:obj.data.purCode},function (result){
                        if (result.success){
                            layer.msg("退货成功", {
                                icon: 1,
                                time: 1000
                            }, function() {
                                //执行重载
                                table.reload('purchase');
                                layer.close(index);
                            });
                        }
                    })
                })
            }
        })

    })




</script>
<script type="text/html" id="repostory">
    {{d.repostoryType.reponseTypeName}}
</script>
<script type="text/html" id="emp">
    {{d.emp.eName}}
</script>
<script type="text/html" id="supplier">
    {{d.supplier.supplierName}}
</script>
<script type="text/html" id="payState">
    {{#if (d.payState == '未支付') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">未支付</button>
    {{# }else if (d.payState == '已支付') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">已支付</button>
    {{# } }}
</script>
<script type="text/html" id="purchaseType">
    {{#if (d.purchaseType == '进货') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">进货</button>
    {{# }else if (d.purchaseType == '退货') { }}
    <button class="pear-btn  pear-btn-sm" style="background-color: #01AAED;color: #FFFFFF">退货</button>
    {{# } }}
</script>
<script type="text/html" id="purchaseState">

    {{#if (d.purchaseState == 0) { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待审批</button>
    {{# }else if (d.purchaseState == 1 && d.purchaseType=='进货') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待审批</button>
    {{# }else if (d.purchaseState == 1 && d.purchaseType=='退货') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待出库</button>
    {{# }else if (d.purchaseState == 2 && d.purchaseType=='进货') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待采购</button>
    {{# }else if (d.purchaseState == 2 && d.purchaseType=='退货') { }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待出库</button>
    {{# }else if(d.purchaseState == 3 && d.purchaseType=='进货'){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待入库</button>
    {{# }else if(d.purchaseState == 3 && d.purchaseType=='退货'){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">待退货</button>
    {{# }else if(d.purchaseState == 4 && d.purchaseType=='进货'){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">已入库</button>
    {{# }else if(d.purchaseState == 4 && d.purchaseType=='退货'){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">已退货</button>
    {{# }else if(d.purchaseState == 5){ }}
    <button class="pear-btn pear-btn-primary pear-btn-sm">已驳回</button>
    {{# } }}
</script>
</body>
</html>